<?php


if($uri == "/api/user/video/user_info"){
    $user_id = $_GET['user_id'];
    $userInfo = $cnSql->read_only("users", "id=$user_id");
    if($userInfo) {
        unset($userInfo['balance']);
        unset($userInfo['password']);
        unset($userInfo['subscribe_phone']);
        unset($userInfo['type']);
        unset($userInfo['create_time']);
        unset($userInfo['update_time']);
    }
    $outJson->data = $userInfo;
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;
}

/**
 * 获取视频列表（排除已观看）
 */
if($uri == "/api/user/video/feeds") {
    $token = $_SERVER['HTTP_ACCESS_TOKEN'];
    $fingerprint = isset($_GET['fingerprint']) ? $_GET['fingerprint'] : '';
    
    // 构建排除条件
    $exclude_condition = "";
    if($token) {
        $userInfo = $cnSql->read_only("users", "token = '$token'");
        if($userInfo) {
            $exclude_condition = "v.id NOT IN (
                SELECT video_id FROM video_views 
                WHERE user_id = $userInfo[id]
            )";
        }
    } else if($fingerprint) {
        $exclude_condition = "v.id NOT IN (
            SELECT video_id FROM video_views 
            WHERE fingerprint = '$fingerprint'
        )";
    }
    
    // 获取未观看的视频
    $videos = $cnSql->custom("
        SELECT v.* 
        FROM video_feeds v 
        WHERE $exclude_condition 
        ORDER BY RAND() 
        LIMIT 5
    ");
    
    $outJson->data = $videos;
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;
}

/**
 * 记录视频观看
 */
if($uri == "/api/user/video/view") {
    $postJson = json_decode(file_get_contents("php://input"));
    $video_id = isset($postJson->video_id) ? intval($postJson->video_id) : 0;
    $token = $_SERVER['HTTP_ACCESS_TOKEN'];
    $fingerprint = isset($postJson->fingerprint) ? $postJson->fingerprint : '';
    $view_type = isset($postJson->view_type) ? intval($postJson->view_type) : 1;
    $duration = isset($postJson->duration) ? intval($postJson->duration) : 0;
    
    $user_id = null;
    if($token) {
        $userInfo = $cnSql->read_only("users", "token = '$token'");
        if($userInfo) {
            $user_id = $userInfo['id'];
        }
    }
    
    // 记录观看信息
    $cnSql->insert_json("video_views", array(
        "video_id" => $video_id,
        "user_id" => $user_id,
        "fingerprint" => $fingerprint,
        "view_type" => $view_type,
        "view_time" => date("Y-m-d H:i:s"),
        "duration" => $duration,
        "ip" => $_SERVER['REMOTE_ADDR'],
        "user_agent" => $_SERVER['HTTP_USER_AGENT']
    ));
    
    $outJson->msg = "记录成功";
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;
}


/**
 * 获取观看历史
 */
if($uri == "/api/user/video/history"){
    $token = $_SERVER['HTTP_ACCESS_TOKEN'];
    $fingerprint = isset($_GET['fingerprint']) ? $_GET['fingerprint'] : '';
    $user_id = null;
    if($token) {
        $userInfo = $cnSql->read_only("users", "token = '$token'");
        if($userInfo) {
            $user_id = $userInfo['id'];
        }
    }
    $history = $cnSql->custom("
        SELECT v.*, f.cover, f.description, f.url, f.likes, f.comments 
        FROM video_views v
        LEFT JOIN video_feeds f ON v.video_id = f.id 
        WHERE v.user_id = $user_id 
        ORDER BY v.view_time DESC 
        LIMIT 10
    ");
    $outJson->data = $history;
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;
}

/**
 * 点赞/取消点赞
 */
if($uri == "/api/user/video/like") {
    $postJson = json_decode(file_get_contents("php://input"));
    $video_id = isset($postJson->video_id) ? intval($postJson->video_id) : 0;
    $token = $_SERVER['HTTP_ACCESS_TOKEN'];
    $fingerprint = isset($postJson->fingerprint) ? $postJson->fingerprint : '';
    
    $user_id = null;
    if($token) {
        $userInfo = $cnSql->read_only("users", "token = '$token'");
        if($userInfo) {
            $user_id = $userInfo['id'];
        }
    }
    
    // 检查是否已点赞
    $where = $user_id ? "video_id = $video_id AND user_id = $user_id" : 
                        "video_id = $video_id AND fingerprint = '$fingerprint'";
    $exists = $cnSql->read_only("video_likes", $where);
    
    if($exists) {
        // 取消点赞
        $cnSql->delete("video_likes", $where);
        $cnSql->custom("UPDATE video_feeds SET likes = likes - 1 WHERE id = $video_id");
        $outJson->liked = false;
    } else {
        // 添加点赞
        $cnSql->insert_json("video_likes", array(
            "video_id" => $video_id,
            "user_id" => $user_id,
            "fingerprint" => $fingerprint,
            "create_time" => date("Y-m-d H:i:s")
        ));
        $cnSql->custom("UPDATE video_feeds SET likes = likes + 1 WHERE id = $video_id");
        $outJson->liked = true;
    }
    
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;
}

/**
 * 获取点赞列表
 */
if($uri == "/api/user/video/likes"){
    $token = $_SERVER['HTTP_ACCESS_TOKEN'];
    $fingerprint = isset($_GET['fingerprint']) ? $_GET['fingerprint'] : '';
    
    if($token) {
        $userInfo = $cnSql->read_only("users", "token = '$token'");
        $where = "l.user_id = $userInfo[id]";
    } else {
        $where = "l.fingerprint = '$fingerprint'";
    }
    
    $likes = $cnSql->custom("
        SELECT v.*, l.create_time as like_time
        FROM video_likes l
        JOIN video_feeds v ON l.video_id = v.id
        WHERE $where
        ORDER BY l.create_time DESC 
        LIMIT 10
    ");
    
    $outJson->data = $likes;
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;
}